package com.rqf.ls.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rqf.ls.service.ICommonDataService;
import com.rqf.ls.service.IRegisterService;
import com.rqf.ls.service.ISignUpService;
import com.server.common.util.ExportExcelUtil;

@Controller
@RequestMapping("export")
public class ExportController {
	
	@Autowired
	private ISignUpService signUpService;
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private IRegisterService registerService;

	/**
	 * 公益活动导出
	 * @param request
	 * @param response
	 * @param phoneNo
	 * @param userType normal/lawyer
	 * @param fileName
	 * @throws IOException
	 * @author ServerZhang
	 * @date 2016年6月2日
	 */
	@RequestMapping("public/{phoneNo}/{userType}/{fileName}")
	public void publicExcel(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String phoneNo,
			@PathVariable String userType,@PathVariable String fileName) throws IOException {

		Boolean isSuccess = true;
		List<Map<String, Object>> list = signUpService.getList(phoneNo, userType);
		
		Map<String, Object> userMap = registerService.read(phoneNo);
		
		String sheetName = "公益活动";
		String[] fieldsKey = {"ACTIVITYNAME","LOCATIONADDR","CREATETIME","NOTE"};
		String[] fieldsName = {"活动名称","活动地点","签到时间","备注"};
		Integer[] width = {20 * 256,35 * 256,20 * 256,30 * 256};
		StringBuffer titleBuffer = new StringBuffer();
		titleBuffer.append("姓名：").append(userMap.get("USERNAME") == null ? "无" : userMap.get("USERNAME").toString())
			.append("  电话：").append(phoneNo)
			.append("  银行名称：").append(userMap.get("BANKNAME") == null ? "无" : userMap.get("BANKNAME").toString())
			.append("  开户行：").append(userMap.get("ACCOUNTBANK") == null ? "无" : userMap.get("ACCOUNTBANK").toString())
			.append("  银行卡号：").append(userMap.get("ACCOUNT") == null ? "无" : userMap.get("ACCOUNT").toString());
		String titleName = titleBuffer.toString();
		HSSFWorkbook workbook = ExportExcelUtil.list2Bytes(titleName, sheetName, fieldsKey, fieldsName, width, list);

		/*
		 * Boolean isSuccess = true; IExcelPrintByte excelPort =
		 * reportExportService.getExcelPort(); byte[] bytes; try { bytes =
		 * excelPort.excelPrintByte(reportCode, refNo); } catch
		 * (IExcelPrintByteExcelPrintByteApplicationServiceErrorFaultFaultMessage
		 * e1) { throw new
		 * WebserviceInvokeException(e1.getFaultInfo().getErrorMessage()); //
		 * e1.printStackTrace(); }
		 */

		OutputStream outputStream = null;
		try {
			// String realFileName = StringUtils.isEmpty(fileName) ? "未知文件名.xls"
			// : URLDecoder.decode(fileName,"utf-8")+".xls";
			String realFileName = StringUtils.isEmpty(fileName) ? "未知文件名.xls"
					: fileName + ".xls";
			realFileName = URLEncoder.encode(realFileName, "UTF-8");
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-Disposition", "attachment; filename=\""
					+ realFileName + "\"");
			//response.addHeader("Content-Length", "" + bytes.length);
			response.setContentType("application/octet-stream;charset=UTF-8");
			outputStream = new BufferedOutputStream(response.getOutputStream());
			workbook.write(outputStream);
			//outputStream.write(bytes);
			outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
			isSuccess = false;
			// throw new FileReadException("文件读取发生错误");
		} finally {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		/*
		 * if (isSuccess) { OutputUtil.returnSuccess(response, isSuccess); }
		 */
	}

}
